package cn.kimming.wms.web.action;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.JsonMsg;
import cn.kimming.wms.domain.Product;
import cn.kimming.wms.query.ProductQueryObject;
import cn.kimming.wms.service.IBrandService;
import cn.kimming.wms.service.IProductService;
import cn.kimming.wms.util.FileUploadUtils;
import lombok.Getter;
import lombok.Setter;

@Controller("productAction")
@Scope("prototype")
public class ProductAction extends BaseAction {
	
	private static final long serialVersionUID = -8784596193420730799L;

	@Autowired
	private IProductService productService;
	@Autowired
	private IBrandService brandService;
	
	@Getter
	private Product product = new Product();
	
	@Getter 
	private ProductQueryObject qo = new ProductQueryObject();

	@Setter
	private File image;	//货品图片
	@Setter
	private String imageFileName;
	
	@Override
	@RequiredPermission("货品列表")
	@InputConfig(methodName="input")
	public String execute() {
		try {
			ActionContext.getContext().put("objs", productService.query(qo));
			ActionContext.getContext().put("brands", brandService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}
	
	@RequiredPermission("货品编辑")
	public String input() {
		try {
			ActionContext.getContext().put("brands", brandService.list());
			if (product.getId() != null) {
				product = productService.get(product.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return INPUT;
	}
	
	
	@RequiredPermission("货品选择列表")
	public String selectProductList() {
		try {
			ActionContext.getContext().put("objs", productService.query(qo));
			ActionContext.getContext().put("brands", brandService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "selectProductList";
	}

	@RequiredPermission("货品保存和更新")
	public String saveOrUpdate() {
		System.out.println(image);
		//货品本来存在图片,而用户又选择了新的图片,则把原来的图片删除
		if (product.getId() != null && image != null && product.getImagePath() != null) {
			FileUploadUtils.delete(product.getImagePath());
		}
		//(1)用户选择了图片, 进行上传;(2)货品本来存在对应图片,用户没有选择新的图片,保持原来的图片
		if (image != null) {
			String imagePath = FileUploadUtils.upload(image, imageFileName);
			product.setImagePath(imagePath);
		}
		if (product.getId() != null) {
			try {
				productService.update(product);
				addActionMessage("更新成功!");
			} catch(Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				productService.save(product);
				addActionMessage("保存成功!");
			} catch(Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	@RequiredPermission("货品删除")
	public String delete() {
		try {
			if (product.getId() != null) {
				if (productService.get(product.getId()).getImagePath() != null) {
					FileUploadUtils.delete(productService.get(product.getId()).getImagePath());
				}
				productService.delete(product.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}

	public void prepareSaveOrUpdate() throws Exception {
		if (product.getId() != null) {
			product = productService.get(product.getId());
		}
	}
}
